package com.kkd.kkdapp.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.MyRegularDebtAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.RegularDebtDetailData;
import com.kkd.kkdapp.http.entity.RegularDebtDetailDataItem;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

/****
 * 
 * @date 2016-03-16
 * 
 * @author mty
 * 
 * @Name 定期历史
 * 
 */
public class HistoryActivity extends BaseActivity implements OnClickListener,
		KKDScrollListener {

	private final int CODE_ZERO = 1;
	private final int CODE_OTHER = 2;

	private RelativeLayout condition_img; // 返回
	private long pageNO = 1;
	private long pageSize = 10;
	private long pageCount;
	private boolean REFRESHFLAG = true;
	private KkdXlistView manage_lv;
	private ListView mListView;
	private ImageView mNullImg;
	private TextView mNullTv;
	private MyRegularDebtAdapter adapter;
	private String desc;
	private int type = 4;
	private List<RegularDebtDetailDataItem> mList = new ArrayList<RegularDebtDetailDataItem>();

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case CODE_ZERO:
				RegularDebtDetailData data = (RegularDebtDetailData) msg.obj;
				if (adapter == null) {
					adapter = new MyRegularDebtAdapter(HistoryActivity.this,
							mList, type);
					mListView.setAdapter(adapter);
				}
				long PageSize = data.getPageSize();
				long totalCount = data.getTotalCount();
				if (totalCount % PageSize != 0) {
					if (totalCount == 0) {
						pageCount = 0;
					} else {
						pageCount = (totalCount / PageSize) + 1;
					}
				} else {
					pageCount = (totalCount / PageSize);
				}
				if (pageNO > pageCount) {
					manage_lv.stopLoadMore(-1);
					if (REFRESHFLAG) {
						manage_lv.stopLoadMore(1);
					}
				} else {
					manage_lv.setNoMore(false);
					if (pageNO == 1) {
						mList.clear();
					}
					List<RegularDebtDetailDataItem> finishList;
					if (data.getFinishList() != null) {
						finishList = data.getFinishList();
					} else {
						finishList = new ArrayList<RegularDebtDetailDataItem>();
					}
					mList.addAll(finishList);
					adapter.notifyDataSetChanged();
					if (finishList.size() < pageSize) {
						manage_lv.stopLoadMore(1);
					} else {
						manage_lv.stopLoadMore(0);
					}
				}
				manage_lv.stopRefresh();
				if (REFRESHFLAG) {
					if (mList.size() == 0) {
						mNullImg.setVisibility(View.VISIBLE);
						mNullTv.setVisibility(View.VISIBLE);
						mNullTv.setText("暂无已还清的定期投资");
						mNullImg.setImageResource(R.drawable.img_null);
					} else {
						mNullImg.setVisibility(View.GONE);
						mNullTv.setVisibility(View.GONE);
					}
				}
				break;
			case CODE_OTHER:
				showToast(desc);
				manage_lv.stopLoadMore(1);
				manage_lv.stopRefresh();
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_history);
		initView();
		setListener();
	}

	/*************
	 * 初始化控件
	 *************/
	public void initView() {
		condition_img = (RelativeLayout) this.findViewById(R.id.condition_img);
		mNullImg = (ImageView) this.findViewById(R.id.id_no_content);
		mNullTv = (TextView) this.findViewById(R.id.id_no_content_text);
		manage_lv = (KkdXlistView) this.findViewById(R.id.manage_lv);
		manage_lv.setKKDScrollListener(this);
		mListView = manage_lv.mListView;
		mListView.setDividerHeight(0);
		manage_lv.startRefresh();
		if (AppUtil.isConnect(this)) {
			requestData();
		} else {
			// mXListView.setPullLoadEnable(false);
			mNullImg.setVisibility(View.VISIBLE);
			mNullTv.setVisibility(View.VISIBLE);
			mNullTv.setText("网络连接失败，请检查网络！");
			mNullImg.setImageResource(R.drawable.img_null);
			manage_lv.stopRefresh();
		}
	}

	/*************
	 * 注册监听事件
	 *************/
	public void setListener() {
		condition_img.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.condition_img:
			finish();
			break;
		}

	}

	/**
	 * 请求接口数据
	 */
	private void requestData() {
		KkdHttpMagager.getInstance().getMyRegularDebtManager(type,
				"5002,5003,5004", pageNO, pageSize,
				RegularDebtDetailData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						Message msg = Message.obtain();
						msg.obj = responseBody;
						msg.what = CODE_ZERO;
						mHandler.sendMessage(msg);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						HistoryActivity.this.desc = desc;
						mHandler.sendEmptyMessage(CODE_OTHER);
					}
				});
	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		requestData();
		REFRESHFLAG = true;
	}

	@Override
	public void onLoadMore() {
		pageNO++;
		requestData();
		REFRESHFLAG = false;
	}
}
